package com.mingtu.guards.adapter;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.mingtu.guards.R;

import java.util.LinkedHashMap;


public class AircraftAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private LinkedHashMap<Integer, Object> hashMap = new LinkedHashMap<>();

    public AircraftAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_charge, null);

            holder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
            holder.ivImg = (ImageView) convertView.findViewById(R.id.iv_img);
            holder.tvText1 = (TextView) convertView.findViewById(R.id.tv_text1);
            holder.tvText2 = (TextView) convertView.findViewById(R.id.tv_text2);


            ViewGroup.LayoutParams lp = holder.layout.getLayoutParams();
            int screenWidth = ScreenUtils.getScreenWidth();
            int mWidth = (screenWidth - SizeUtils.dp2px(15) * 2 - SizeUtils.dp2px(6) * 1) / 2;
            int mHeight = mWidth * SizeUtils.dp2px(120) / SizeUtils.dp2px(175);
            lp.width = mWidth;
            lp.height = mHeight;
            holder.layout.setLayoutParams(lp);


            holder.layout.setOnClickListener(this);
            holder.layout.setTag(position);
            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();
        }


        switch (position) {
            case 0:
                holder.ivImg.setBackgroundResource(R.mipmap.img_monitor);
                holder.tvText1.setText("实时监测");
                holder.tvText2.setText("Realtime monitoring ");

                break;
            case 1:
                holder.ivImg.setBackgroundResource(R.mipmap.img_history_playback);
                holder.tvText1.setText("历史回放");
                holder.tvText2.setText("History playback");
                break;

//            case 2:
//                holder.ivImg.setBackgroundResource(R.mipmap.img_device_management);
//                holder.tvText1.setText("设备管理");
//                holder.tvText2.setText("Equipment management");
//                break;
        }


        return convertView;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            try {
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    class ViewHolder {
        public LinearLayout layout;
        public ImageView ivImg;
        public TextView tvText1;
        public TextView tvText2;
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

}
